<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>我的</title>
	<link rel="icon" href="img/icon.png" type="image/x-icon"/>
	<link rel="stylesheet" type="text/css" href="css/mui.css"/>
	<link rel="stylesheet" type="text/css" href="css/mui.min.css"/>
	<link rel="stylesheet" type="text/css" href="css/app.css"/>
</head>
<body>
<!-- 顶部菜单 -->
<ul id="top">
	<!-- <li class="addBug back" onclick="mui.back();">返回</li> -->
	<li class="addBug back">&nbsp;</li>
	<li class="addBug">我的设置</li>
	<li class="addBug back">&nbsp;</li>
</ul>
<!-- 数据列表 -->
<div style="height: 20px;"></div>
<table cellspacing="0" class="bugShow">
	<tr>
		<th colspan="2">
		<!-- <th colspan="2" id="adminHead" onclick="jumpToMyHead()"> -->
			<img src="img/icon.png" width="100" height="100">
		</th>
	</tr>
	<tr>
		<th colspan="2" onclick="jumpToMyAdminName()">
			<span id="nickname"></span>
		</th>
	</tr>
	<tr>
		<td class="tl">项目设置</td>
		<td class="btn" onclick="jumpToMyProList()">
			<span id="proName"></span>
		</td>
	</tr>
	<tr>
		<td class="tl">接口域名</td>
		<td class="btn" onclick="jumpToMyDomain()">
			域名-详情
		</td>
	</tr>
	<tr>
		<td class="tl">需 &nbsp; &nbsp; &nbsp; 求</td>
		<td class="btn" id="proDetail" onclick="jumpToMyProDetail()">
			项目初始化-详情
		</td>
	</tr>
	<tr>
		<td class="tl">版本升级</td>
		<td class="btn">
			<span id="version"></span>
			<span id="verNew"></span>
		</td>
	</tr>
	<tr>
		<td class="tl">管理员</td>
		<td class="btn" onclick="jumpToMyAdmin()">列表详情</td>
	</tr>
	<tr>
		<td class="tl">缓存</td>
		<td class="btn" onclick="jumpToMyCache()">列表详情</td>
	</tr>
	<tr>
		<td class="tl">密码更新</td>
		<td class="btn" onclick="jumpToMyAdminPwd()">详情</td>
	</tr>
	<tr>
		<td class="tl">登陆</td>
		<td class="btn">
			<div id="tologin" onclick="jumpToLogin()">
				未登陆？去登陆
			</div>
			<div id="tologout" onclick="setLogout()">
				已登陆？去退出
			</div>
		</td>
	</tr>
</table>
<div style="height: 50px;"></div>
<!-- 底部菜单 -->
<ul id="foot">
	<li class="noclick" onclick="jumpToIndex()">BUG</li>
	<li class="noclick" onclick="jumpToHistory()">历史</li>
	<li class="noclick" onclick="jumpToMore()">更多</li>
	<li onclick="jumpToMy()">我的</li>
</ul>
</body>
</html>


<script type="text/javascript" src="js/mui.min.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript">
	var domain = getDomain();
	var token = cacheGet(adminTokenKey);
	var appVersion = cacheGet(appVersionKey);
	var adminInfo = cacheGet(adminInfoKey);
	var proInfo = cacheGet(proInfoKey);
	//数据接口
	(function($, owner) {
		getAdminInfo();
		getProInfo();
		getAppVersion();
		isLogin();
		getNewVersion();  //获取APP最新版本
	}(mui, window.app = {}));
	//用户信息获取
	function getAdminInfo(){
		var nickName = '管理员昵称';  //默认昵称
		if (adminInfo && adminInfo.adminName) {
			nickName = adminInfo.adminName;
		}
		mui('#nickname')[0].innerHTML = nickName;
	}
	//项目信息
	function getProInfo(){
		var proId = 1;
		var proName = '?美策服装?';
		var proDetail = '?项目初始化-详情?';
		if (!proInfo) {
			proInfo = {
				'proId':proId,
				'proName':proName,
				'proDetail':proDetail
			};
			cacheSet(proInfoKey,proInfo);
		} else {
			proId = proInfo.proId;
			proName = proInfo.proName;
			proDetail = proInfo.proDetail;
		}
		mui('#proName')[0].innerHTML = proName;
		mui('#proDetail')[0].innerHTML = proDetail;
	}
	//APP版本
	function getAppVersion(){
		var version = '1';  //版本号
		if (appVersion) { version = appVersion.version; }
		mui("#version")[0].innerHTML = 'V'+version;
	}
	//登陆判断
	function isLogin(){
		if (!token) {
			mui("#tologin")[0].style.display = "block";
			mui("#tologout")[0].style.display = "none";
		} else {
			mui("#tologin")[0].style.display = "none";
			mui("#tologout")[0].style.display = "block";
		}
	}
	//退出登陆
	function setLogout(){
		cacheDel(adminTokenKey);
		cacheDel(adminInfoKey);
		//成功提示
		mui.toast('退出成功！',{ 
			duration:'long', type:'div',
		});
		//页面跳转至登陆
		setTimeout(function(){
			jumpToMyLogin();
		}, 2000);
	}
	//获取APP最新版本
	function getNewVersion(){
		if (!appVersion) { appVersion = '1'; }
		mui.ajax(domain+'/app/version/new',{
			data:{
				//
			},
			dataType:'json',//服务器返回json格式数据
			type:'post',//HTTP请求类型
			timeout:10000,//超时时间设置为10秒；
			// headers:{'Content-Type':'application/json'},
			headers:{'Content-Type':'application/x-www-form-urlencoded'}, 
			async: true,  //async: true || false, // 异步 || 同步
			crossDomain: true,  //强制跨域
			success:function(res){
				console.log(res);
				var ver = '';
				if (res.code!=0) {
					ver += '<b>无新版本</b>';
					mui("#verNew")[0].innerHTML = ver;
					return
				}
				var version = res.data.version;
				var link = res.data.link;
				if (res.data.version <= appVersion) {
					ver += '<b>无新版本</b>';
					mui("#verNew")[0].innerHTML = ver;
					return
				}
				ver += '<b style="color:red;">有新版本</b>';
				mui("#verNew")[0].innerHTML = ver;
				//弹出对话框
				mui.confirm('需要升级版本么？','新版升级提示',['取消','确认'],function (e) {
					// console.log(e.index,version,link);
					if (e.index==1) {
						setDownload(version,link);
					}
				},'div');
			},
			error:function(xhr,type,errorThrown){
				// console.log('网络错误');
				mui.toast(xhr+type+'网络错误！',{
					duration:'long', type:'div',
				});
			}
		});
	}
	//APP下载安装
	function setDownload(version,link){
		// console.log(version,link); return
		mui.plusReady(function () {
			// plus.runtime.openWeb(link);
			// plus.runtime.openURL(link);  //下载
			plus.nativeUI.toast("正在准备下载，请稍后！");
			var dtask = plus.downloader.createDownload(link, {}, function (d, status) {
				if (status == 200) {
					mui.toast('下载成功，准备安装！',{ 
						duration:'long', type:'div',
					});
					var path = d.filename;
					plus.runtime.install(path); // 自动安装apk文件
					mui.toast('安装成功！',{ 
						duration:'long', type:'div',
					});
				} else {
					plus.nativeUI.alert('版本更新失败:' + status);
				}
			});
			dtask.start();
			//下载安装进度显示
			dtask.addEventListener("statechanged", function(task, status) {
				switch(task.state) {
					case 1: // 开始
						console.log('开始');
						mui.toast('开始！',{ 
							duration:'long', type:'div',
						});
						break;
					case 2: //已连接到服务器
						console.log('已连接到服务器');
						mui.toast('已连接到服务器！',{ 
							duration:'long', type:'div',
						});
						break;
					case 3: // 已接收到数据
						console.log('已接收到数据');
						var current = parseInt(100 * task.downloadedSize / task.totalSize);
						plus.notification.setProgress(current); //插件调用
						mui.toast('下载进度：'+current+'！',{ 
							duration:'long', type:'div',
						});
						break;
					case 4: // 下载完成 
						console.log('下载完成 ');
						mui.toast('下载完成',{ 
							duration:'long', type:'div',
						});
						plus.notification.compProgressNotification("下载完成"); //插件调用
						plus.runtime.install(task.filename, {}, function() {
							//成功安装回调，执行重启app操作
							plus.runtime.restart();
						}, function(DOMException) {
							console.log(JSON.stringify(DOMException));
							mui.toast("安装失败");
						});
						break;
				}
			});
			//更新本地APP版本缓存
			var appVersion = {
				'version':version,
				'link':link
			};
			cacheSet(appVersionKey,appVersion);
		});
	}
</script>